package LeetcodeTest_three;
//两数相加，链表
public class addTwoNumbers
{
    public static void main(String[] args)
    {
        ListNode l1=new ListNode(5);
        ListNode tail1=null;
        tail1=l1;
        tail1.next=null;
        ListNode l2=new ListNode(5);
        ListNode tail2=null;
        tail2=l2;
        tail2.next=null;
        ListNode l3=solution.addTwoNumbers(l1,l2);
        while(l3!=null)
        {
            System.out.println(l3.val);
            l3=l3.next;
        }
    }
}

class solution
{
    public static  ListNode addTwoNumbers(ListNode l1, ListNode l2)
    {
        int a, b, m, n;
        a = 0;
        ListNode l3 = new ListNode(0);
        ListNode tail = null;
        tail = l3;
        while (l1 != null || l2 != null)
        {
            if (l1 != null)
                m = l1.val;
            else
                m = 0;
            if (l2 != null)
                n = l2.val;
            else
                n = 0;
            b = (m + n + a) % 10;
            a = (m + n + a) / 10;
            ListNode p = new ListNode(b);
            tail.next = p;
            tail = tail.next;
            if (l1 != null)
                l1 = l1.next;
            if (l2 != null)
                l2 = l2.next;
        }
        if (a > 0)
        {
            tail.next = new ListNode(a);
            tail=tail.next;
        }
        return l3.next;
    }
}
class ListNode
{
   int val;
   ListNode next;
   ListNode(int x) { val = x; }
}
